package com.jl.js.process.command;

import java.util.concurrent.ArrayBlockingQueue;

public class CommandInvoker {

	/** invoker's name */
	private String name;

	private ArrayBlockingQueue<ICommand> cmds = new ArrayBlockingQueue<ICommand>(
			10);

	public CommandInvoker(String name) {
		this.name = name;
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						ICommand cmd = cmds.take();
						cmd.execute();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
	}

	public void invoke(ICommand cmd) {
		try {
			cmds.put(cmd);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof CommandInvoker) {
			return toString().equals(obj.toString());
		}
		return false;
	}

	@Override
	public int hashCode() {
		return name.hashCode();
	}

	@Override
	public String toString() {
		return "Invoker " + name;
	}

}
